<?php
/*
Plugin Name: ArticleRec
Plugin URI: http://www.xys-reader.org/blogs
Description: Let mutil-blog editors recommend articles on start pages
Version: 0.10
Author: LIU Xuefeng
Author URI: http://www.xy-reader.og
*/


add_filter('the_content', 'site_rec_filter');
function site_rec_filter($content) {


        global $blog;
	$rec_cat_id=28067;

        get_currentuserinfo();

        $myblogs = get_userblogs();
        $sys_admin_user=0;
        if ($myblogs) {
                foreach ($myblogs as $b)
                        if($b->id ==2) $sys_admin_user=1;
        }
        if($sys_admin_user ){

    global $id, $post;
    $uid=$post->guid;
    preg_match("/blogs\/([^\/?]*)/",$uid,$matched);
	
    $url="http://www.de-sci.org/blogs/".$matched[1]."/?p=".$post->ID;
    $special_edit="\n<div align=\"right\" >\n<form action=\"http://www.de-sci.org/xys/p_add_product_to_cat_by_link.php\" method=\"post\" target=\"_new\">";

    $special_edit.="<input type=\"hidden\" name=\"parent_cat_id\" value=\"$rec_cat_id\">\n";
    $special_edit.="<input type=\"hidden\" name=\"arc_title\" value=\"".$post->post_title."\">\n";
    $special_edit.="<input type=\"hidden\" name=\"arc_link\" value=\"$url\">\n";
    $special_edit.="<input type=\"hidden\" name=\"arc_author\" value=\"".$post->author_nickname."\">\n";
    $special_edit.="<input type=\"hidden\" name=\"arc_date\" value=\"".$post->post_modified."\">\n";
    $special_edit.="<input type=\"hidden\" name=\"arc_ID\" value=\"".$post->ID."\">\n";
    $special_edit.="<input type=\"submit\" name=\"arc_submit\" value=\"Recommend This One\">";
    $special_edit.="</form>\n</div>\n\n";

   //$special_edit.=print_r($post,1);
   //$special_edit.=print_r($blog,1);

  }
  if(!is_home()) $content=$special_edit.$content.$special_edit;
/*

*/
	return $content;

}
function show_site_rec($rec_title,$record_num=3,$cat_num=2, $cat_id=NULL ){
	global $wpdb, $blog,$more_article;


	$output="";

	if(!$more_article){
		$qry_cond=" limit 0, $cat_num";
	}

//catalogs.other like \"%SHOW:ON%\" and 

$qry="SELECT catalogs.title, catalogs.alias,catalogs.cat_id, max(product_detail.date) as arc_date
FROM catalogs 
inner join products on products.cat_id=catalogs.cat_id
inner join product_detail on product_detail.product_id=products.product_id
where 
catalogs.parent_cat=\"28066\"
group by catalogs.cat_id order by arc_date desc
$qry_cond
";
	$arc_result = $wpdb->get_results($qry);
	foreach($arc_result as $re){

		$id_list="";
		$qry="SELECT product_detail.other as id FROM product_detail inner join products where products.product_id=product_detail.product_id and products.cat_id=\"".$re->cat_id."\" ";
		$arc_re2 = $wpdb->get_results($qry);
		foreach($arc_re2 as $re2){
		   if($re2->id){
			   if($id_list)
				$id_list.=", $re2->id";
			   else
				$id_list.="$re2->id";
		   }
		}
		//if(!$hot_id_list) $hot_id_list=$id_list; else $hot_id_list.=", $id_list";

		$title=iconv("gbk","utf-8",$re->title);

		$output.="<!-- (1) -->\n\n";
		if($cat_id==$re->cat_id)
		$output.=get_site_rec($title,$id_list,-1,$re->cat_id);	
		else
		$output.=get_site_rec($title,$id_list,$record_num,$re->cat_id);	

		//$hot_list.='<li><a href="?cat_id='.$re->cat_id.'#'.$re->cat_id.'">'.$title.' </a></li>';
		
	}	

		//$hot_list.="</ul>\n <div style=\"clear:both;\"></div>\n ";

	$re='<fieldset id="site-rec-list" class="left_side_width"> ';
	if($more_article){
	   //$re=$hot_list;
	   $re="";
	   $re.='<fieldset id="site-rec-list" class="left_side_width_wide"> ';
	}

	$re.=' <legend><font color="red"><b>'.$rec_title.'</b></font> </legend>
	      <div  style="background-image: url(hot_image/flower.jpg2); background-repeat: no-repeat; background-position: top right; margin:0;padding:0; ">';

	$re.=$output;

	$re.="</div>\n </fieldset>\n";

	return $re;
}

function get_site_rec($title, $id_list, $record_num=10,$cat_id=NULL){

	global $wpdb, $blog,$more_article;
	$hot_title = $title;
	//$post_list="\n\n <!-- ";
	if($record_num>0) $qry_num=" limit 0,$record_num";

	$my_q="SELECT distinct `$wpdb->posts`.ID, `$wpdb->posts`.post_author as user_id,`$wpdb->posts`.post_name, `$wpdb->posts`.post_title, `$wpdb->posts`.guid, 
$wpdb->blogs.slug,
`$wpdb->posts`.post_date_gmt,
`$wpdb->posts`.post_date,
`$wpdb->posts`.post_content as content
from `$wpdb->posts`
inner join `$wpdb->post2cat` on `$wpdb->posts`.ID = `$wpdb->post2cat`.post_id
inner join `$wpdb->categories` on `$wpdb->post2cat`.category_id = `$wpdb->categories`.cat_ID
inner join `$wpdb->blogs` on `$wpdb->categories`.blog =`$wpdb->blogs`.id
inner join  product_detail on product_detail.other = $wpdb->posts.ID  
where $wpdb->posts.ID in ($id_list) 
and `$wpdb->blogs`.status=\"active\"
and `$wpdb->posts`.post_status=\"publish\"
and TO_DAYS(NOW()- INTERVAL 9 HOUR ) - TO_DAYS( $wpdb->posts.post_date_gmt ) > -1
order by post_date_gmt desc $qry_num";

//order by product_detail.date desc $qry_num";
//order by post_date desc $qry_num";


//DATE_FORMAT( $wpdb->posts.post_date_gmt  + interval 9 HOUR,\"%b %D, %Y\") as p_date

	$i=0;
      	$posts = $wpdb->get_results($my_q);

	if($posts){

		if(!$more_article)
		$end_str= '<a href="show_cat.php?CAT_NAME='.urlencode($cat_name).'"><font size=2>完全列表</font></a>';


		//$output.= "<h3><a name=$cat_id ></a>".$hot_title." &nbsp;  </h3>\n";
		$output.= "<ul> \n";
	        foreach ($posts as $p){
			$i++;

			$title=$p->post_title;
    			mb_internal_encoding("UTF-8");//PEAK
			if(mb_strlen($title)>26 && 0){
				$title=mb_substr($title,0,26);
				$title.="...";
			}

			$url= "http://www.de-sci.org/blogs/".$p->slug ."/?p=".($p->ID);
			$profileuser = new WP_User($p->user_id);

			$output.= "<!-- article begin -->\n";
			$output.= "<li style=\"margin:10px;\">\n";
			$output.= " <b>".$profileuser->display_name;
			$output.= "</b>: <a href=\"".$url."&_rec=1\">". $title;
			$output.= "</a>\n";
	//		$output.= "<font size=2>(";
	//		$output.= substr($p->post_date,5,5) . "</font>)\n" ;

			//$output.= "<br><br>\n"; $output.= "\n"; $output.= concise_content($p->content,170,1);

			$output.= "</li>\n";
			$output.= "<!-- article end -->\n";
			//$post_list.="$p->ID ; $p->post_title ; $url\n";
		}
		$output.= "</ul><!-- end here -->\n\n";

	}

/*
	if($_GET["more_site_rec"])
		$output.= '<a href="./"><br>BACK</a>';
	else
		if($record_num>0) $output.= "&nbsp;&nbsp;<a href=\"./show_hot.php?cat_id=$cat_id#$cat_id\"> More ... </a>";
*/

	//$post_list.="-->\n";
	return $output;//.$post_list;

}
?>
